// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey.layouts;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.Dimension;
import com.incrediblesoft.redjey.Component;
import com.incrediblesoft.redjey.Container;

public class RowLayout implements Layout
{
    /*public RowLayout()
    {
    } */

    public void layoutContainer(Container container)
    {
        int[] insets = container.getInsets();
        int y = insets[2];

        Dimension clientSize = container.getClientSize();

        int componentWidth;
        int componentHeight;

        for (int i=0; i<container.getComponentCount(); i++)
        {
            Component c = container.getComponent(i);

            Dimension preferredSize = c.getPreferredSize();
            componentWidth = preferredSize.width;
            componentHeight = preferredSize.height;

            if (preferredSize.width > clientSize.width ||
                (c.getAlignment() & Component.H_EXPAND) != 0)
                componentWidth = clientSize.width;
             if (preferredSize.height > clientSize.height)
                componentHeight = clientSize.height;

            //if (c instanceof TextPane)
            //System.out.print("Setting textpane "+componentWidth+"x"+componentHeight);
            c.setSize(componentWidth, componentHeight);
            //if (c instanceof TextPane)
            //System.out.println("Textpane now is "+c.getWidth()+"x"+c.getHeight());

            if ((c.getAlignment() & Component.HCENTER_ALIGNMENT) != 0 || (container.getContentAlignment() & Component.HCENTER_ALIGNMENT) > 0)
                c.setX( (clientSize.width - c.getWidth()) / 2 );
            else if ((c.getAlignment() & Component.RIGHT_ALIGNMENT) != 0 || (container.getContentAlignment() & Component.RIGHT_ALIGNMENT) > 0)
                c.setX( clientSize.width - c.getWidth() );
            else
                c.setX(insets[0]);

            c.setY(y);

            //if ((c.getAlignment() & Component.H_EXPAND) != 0)
                //c.setSize(containerWidth, c.getHeight());

            //usedHeight += c.getHeight();
            y += componentHeight;
        }

    }

    public Dimension preferredLayoutSize(Container container)
    {
         int maxWidth = 0, prefHeight = 0;

        for (int i=0; i<container.getComponentCount(); i++)
        {
            Component c = container.getComponent(i);
            maxWidth = Math.max(maxWidth, c.getPreferredSize().width);
            prefHeight += c.getPreferredSize().height;
        }

        return new Dimension(maxWidth, prefHeight);
    }


}
